﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace StockMarketTool.Helper
{
    public class CheckTimeHelper
    {
        public static bool ReachAlarmTime = false;//到达提示时间
        public static bool NeedRefresh = true;
        private static bool ischeckingtime = false;
        private static bool flag = true;
        public static void CheckTime()
        {
            if (ischeckingtime)
                return;
            ischeckingtime = true;
            Thread thread = new Thread(() =>
            {
                while (flag)
                {
                    //提前提醒收市
                    if (DateTime.Now >= new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 14, 50, 0)
                    && DateTime.Now <= new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 15, 0, 0)
                    && !ReachAlarmTime)
                    {
                        ReachAlarmTime = true;
                        AlertMsgWin.GetInstance().Show("骚年，快收市了哟");
                    }
                    if (CacheInfo.UpdateWhenMarketClosed)
                        NeedRefresh = true;
                    //还没开市，刷着也没意思
                    else if (DateTime.Now < new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 9, 30, 0))
                    {
                        NeedRefresh = false;
                    }
                    //休息中，刷着也没意思
                    else if (DateTime.Now > new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 11, 30, 0)
                    && DateTime.Now < new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 13, 0, 0))
                    {
                        NeedRefresh = false;
                    }
                    //下午三点闭市，刷新也没意义了
                    else if (DateTime.Now >= new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 15, 0, 0))
                    {
                        NeedRefresh = false;
                        flag = false;
                    }
                    else
                        NeedRefresh = true;
                    Thread.Sleep(1000);
                }
            });
            thread.IsBackground = true;
            thread.Start();
        }
    }
}
